package com.leyou.controller;

import com.leyou.dto.SpecGroupDTO;
import com.leyou.dto.SpecGroupFormDTO;
import com.leyou.pojo.SpecGroup;
import com.leyou.service.SpecGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jshstart
 * @creat 2020-12-11 13:00
 */
@RestController
@RequestMapping("/spec")
public class SpecGroupController {
    @Autowired
    private SpecGroupService specGroupService;
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querGroupByCatagory(@RequestParam("id") Long id){
        List<SpecGroup> specGroupList = specGroupService.query().eq("category_id", id).list();
      return ResponseEntity.ok(specGroupList.stream().map(SpecGroupDTO::new).collect(Collectors.toList())) ;
    }
    @PostMapping("/group")
    public ResponseEntity<Void> addSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        specGroupService.save(specGroupDTO.toEntity(SpecGroup.class));
        return ResponseEntity.ok().build();
    }
    @PutMapping("/group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        specGroupService.updateById(specGroupDTO.toEntity(SpecGroup.class));
        return ResponseEntity.status(204).build();
    }
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupParamByQCategoryId(@RequestParam("id") Long id){
       List<SpecGroupDTO> specGroupDTOLsit= specGroupService.queryGroupParamByQCategoryId(id);
       return ResponseEntity.ok(specGroupDTOLsit);
    }
}
